/**
 * 详情页
 *
 */
$(function () {
  function getScrollTop() {
    var scrollTop = 0;
    if (document.documentElement && document.documentElement.scrollTop) {
      scrollTop = document.documentElement.scrollTop;
    } else if (document.body) {
      scrollTop = document.body.scrollTop;
    }
    return scrollTop;
  }

  // 吸顶
  window.onscroll = function () {
    if (getScrollTop() > 1000) {
      $("#apitab").addClass("fixed");
    } else {
      $("#apitab").removeClass("fixed");
    }
  };
  // 自定义滚动条(代码显示)
  $(window).on("load", function () {
    $(".codeitem").mCustomScrollbar({ theme: "inset-2-dark", axis: "y" });
  });

  var vm = new Vue({
    el: ".vue-details",
    data: {
      // 统计数据
      statistics: {
        projectSum: 0,
        totalCalls: 0,
        todaySum: 0,
        browseSum: 0,
      },
      userName: '',
      details: {
        id: 1,
        name: null,
        apipic: "",
        apiidesc: "",
        clicknum: 0,
        collectnum: 0,
        usednum: 0,
        apitags: [],
        url: "",
        key: "",
        context: "",
        demourl: "",
        apidoc: {

        },
        apicode: [],
        apierror: [
          {
            title: null,
            desc: null,
          },
        ],
      },
      reviewContent: "",
      relation: [],
      apiUrl: window.location.href.indexOf('127.0.0.1') > -1 ? 'http://39.106.120.139' : ''
    },
    created: function () {
      console.log(Hu.Cookies.get('HuUserName'))
      this.userName = Hu.Cookies.get('HuUserName') || ''
      this.getHotKey(); // 获取热词
      const id = this.getQueryString("id");
      if (id) {
        this.getDetails(id);
        this.getRelationProject(id);
      }
    },
    mounted: function () {
      $("#apitab a").on("click", function () {
        $(this).parent().parent().find("a").removeClass("on");
        $(this).addClass("on");
        var index = $(this).parent().index();
        console.log(index);
        var oTop = $("#apibox").children(".myli").eq(index).offset().top;
        console.log(oTop);
        // $("body").animate({scrollTop:oTop},1000)
        // $("#apibox").children("div").hide();
        document.documentElement.scrollTop = oTop > 80 ? oTop - 80 : oTop;
        // $("#apibox").children("div").eq(index).show();
      });

      function path() {
        var args = arguments,
          result = [];
        for (var i = 0; i < args.length; i++)
          result.push(args[i].replace("$", "./js/syntaxhighlighter/scripts/"));
        return result;
      }
    },
    methods: {
      getHotKey: function () {
        const _this = this;
        $.ajax({
          url: this.apiUrl + "/ProjectApi/Indexdata",
          // url: "http://39.106.120.139/ProjectApi/Indexdata",
          data: {},
          type: "get",
          cache: false,
          dataType: "json",
          success: function (data) {
            console.log(data.data);
            if (data.data.hotkeys) {
              // _this.hotkeys = data.data.hotkeys;
              // _this.categorys = data.data.categorys;
              _this.statistics = data.data.statistics;
            }
          },
          error: function (e) {
            // view("异常！");
            console.log('error', e);
            // alert("异常！");
          },
        });
      },
      getQueryString: function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
          return unescape(r[2]);
        }
        return null;
      },
      getDetails: function (id) {
        const _this = this;
        $.ajax({
          url: this.apiUrl + "/ProjectApi/GetDetail",
          // url: "http://39.106.120.139//ProjectApi/GetDetail",
          data: {
            id: id,
            user_id: Hu.Cookies.get("HuUserID") ? Hu.Cookies.get("HuUserID") : ''
          },
          type: "get",
          cache: false,
          dataType: "json",
          success: function (data) {
            console.log(data.data);
            if (data.data) {
              _this.details = data.data;
              document.title = data.data.name
            }
          },
          error: function (e) {
            // view("异常！");
            console.log('error', e);
            // alert("异常！");
          },
        });
      },
      getRelationProject: function (id) {
        const _this = this;
        $.ajax({
          url: this.apiUrl + "/ProjectApi/GetRelationProject",
          // url: "http://39.106.120.139/ProjectApi/GetRelationProject",
          data: {
            id: id,
          },
          type: "get",
          cache: false,
          dataType: "json",
          success: function (data) {
            console.log(data.data);
            if (data.data) {
              _this.relation = data.data;
            }
          },
          error: function (e) {
            // view("异常！");
            // alert("异常！");
            console.log(e);
            _this.relation = [];
          },
        });
      },
      getClass: function (lan) {
        if (typeof lan !== "string") {
          return;
        }
        var _lan = lan.toLowerCase();
        return _lan
      },
      changedCode: function (e) {
        $(e.target).parent().parent().find("a").removeClass("on");
        $(e.target).addClass("on");
        var index = $(e.target).parent().index();
        var coderequest = $(e.target).parent().parent().parent().parent();
        coderequest.find(".codeitem").hide();
        coderequest.find(".codeitem").eq(index).show();
      },
      clickCollectBtn: function (e) {
        if(!Hu.Cookies.get('HuUserID')) return;
        var _this = $(e.target);
        const that = this;
        if (_this.hasClass('on')) type = 0;
        else type = 1;
        $.ajax({
          url: this.apiUrl + "/projectApi/Liks",
          // url: "http://39.106.120.139/ProjectApi/GetRelationProject",
          data: {
            user_id: Hu.Cookies.get("HuUserID"),
            project_id: this.getQueryString("id"),
            state: type
          },
          type: "get",
          cache: false,
          dataType: "json",
          success: function (data) {
            console.log(data);
            if (type == 1) {
              text = '收藏';
              _this.parent().addClass('on')
              _this.addClass('on');
            }
            else {
              text = '取消收藏';
              _this.parent().removeClass('on')
              _this.removeClass('on');
            }
            $.zxxbox.remind(text + '成功！', null, { title: '提示', drag: true, bg: false, delay: 500 });
            that.getDetails(that.getQueryString("id"))
          },
          error: function (e) {
            // view("异常！");
            // alert("异常！");
            console.log(e);
            $.zxxbox.remind('操作失败！', null, { title: '提示', drag: true, bg: false, delay: 500 });
            that.getDetails(that.getQueryString("id"))
          },
        });
      },
      submitReview: function (e) {
        const _this = this;
        if (!this.reviewContent) {
          $.zxxbox.remind('反馈信息不能为空哦', null, { title: '提示', drag: true, bg: false, delay: 800 });
          return;
        }
        console.log({
          id: this.details.id,
          reviewContent: this.reviewContent
        })
        $.ajax({
          url: "../../code/data/review.json",
          data: {
            id: this.details.id,
            reviewContent: this.reviewContent
          },
          type: "get",
          cache: false,
          dataType: "json",
          success: function (data) {
            console.log(data);
            $.zxxbox.remind('提交成功', null, { title: '提示', drag: true, bg: false, delay: 800 });
            _this.reviewContent = ""
          },
          error: function () {
            // view("异常！");
            $.zxxbox.remind('提交失败', null, { title: '提示', drag: true, bg: false, delay: 800 });
          },
        });
      },
      randomColor: function () {
        let r = Math.floor(Math.random() * 256)
        let g = Math.floor(Math.random() * 256)
        let b = Math.floor(Math.random() * 256)
        return "rgb(" + r + ',' + g + ',' + b + ")"
      }
    },
  });
});
